!
! Copyright (C) 2000-2013 D. Sangalli and the YAMBO team 
!              https://code.google.com/p/rocinante.org
! 
! This file is distributed under the terms of the GNU 
! General Public License. You can redistribute it and/or 
! modify it under the terms of the GNU General Public 
! License as published by the Free Software Foundation; 
! either version 2, or (at your option) any later version.
!
! This program is distributed in the hope that it will 
! be useful, but WITHOUT ANY WARRANTY; without even the 
! implied warranty of MERCHANTABILITY or FITNESS FOR A 
! PARTICULAR PURPOSE.  See the GNU General Public License 
! for more details.
!
! You should have received a copy of the GNU General Public 
! License along with this program; if not, write to the Free 
! Software Foundation, Inc., 59 Temple Place - Suite 330,Boston, 
! MA 02111-1307, USA or visit http://www.gnu.org/copyleft/gpl.txt.
!
subroutine WF_apply_symm(isc,WF_symm)
 !
 use pars,          ONLY:SP
 use wave_func,     ONLY:wf,wf_state
 use FFT_m,         ONLY:fft_rot_r,fft_size
 use electrons,     ONLY:n_spinor
 use D_lattice,     ONLY:nsym,spin_sop,i_time_rev,idt_index
 !
 integer     :: isc(4)
 complex(SP) :: WF_symm(fft_size,n_spinor)
 !
 integer     :: i_wf(n_spinor)
 !
 i_wf(1)=wf_state(isc(1),isc(2),isc(4))
 if(n_spinor==2)  i_wf(n_spinor)=wf_state(isc(1),isc(2),n_spinor)
 !
 if(isc(3)==idt_index) then
   WF_symm(:,1)=wf(:,i_wf(1))
   if(n_spinor==2) WF_symm(:,2)=wf(:,i_wf(2))
   return
 endif
 !
 WF_symm(:,1)=wf(fft_rot_r(isc(3),:),i_wf(1))
 !
 if (n_spinor==2) then
   WF_symm(:,2)=wf(fft_rot_r(isc(3),:),i_wf(2))
   do ifft=1,fft_size
     WF_symm(ifft,1)=spin_sop(1,1,isc(3))*WF_symm(ifft,1)+spin_sop(1,2,isc(3))*WF_symm(ifft,2)
     WF_symm(ifft,2)=spin_sop(2,1,isc(3))*WF_symm(ifft,1)+spin_sop(2,2,isc(3))*WF_symm(ifft,2)
   enddo
 endif
 !
 if(isc(3)>nsym/(i_time_rev+1)) WF_symm=conjg(WF_symm)
 !
 return
 !
end subroutine
